Embed Macro topic
EmbedMacro Documentation
EmbedMacro is a macro that embeds asset files directly into Dart code as byte arrays. It scans a
specified directory, converts matching files into Uint8List data, and generates a virtual file
system API—similar to dart:io—for seamless and efficient access to embedded assets at runtime.
Unlike path-based solutions, EmbedMacro fully embeds asset contents into the generated Dart code,
making it ideal for environments where file system access is limited or unavailable.
Features
- ✅ Embedded Assets: Converts asset files into
Uint8Listbyte arrays in generated Dart code - ✅ Virtual File System: Provides
fileanddirectoryAPIs modeled afterdart:io - ✅ Change Detection: Tracks asset changes using
.embed_generated.jsonand regenerates only modified files - ✅ Recursive Scanning: Optionally embeds files from nested directories
- ✅ Extension Filtering: Supports embedding specific file types or all files
- ✅ Memory Management: Allows explicit disposal of embedded file data when no longer needed
Setup
Register the macro in your main.dart file (or wherever you initialize macros):
void main() async {
await runMacro(
macros: {
'EmbedMacro': EmbedMacro.initialize,
},
assetMacros: {
'assets': [
AssetMacroInfo(
macroName: 'EmbedMacro',
extension: '*',
output: 'lib/embed',
config: const EmbedMacroConfig().toJson(),
),
],
},
);
}
Usage in Code
final fs = EmbedFs.current;
// Read a file as bytes
final file = fs.file('/images/logo.png');
final bytes = file.readAsBytesSync();
// Dispose when done to free memory
file.dispose
();
// List directory contents
final dir = fs.directory('/images');
final files = dir.listSync(recursive: true);
Configuration Parameters
config
- Type:
Map<String, dynamic>? - Required: No
- Description: Configuration object for
EmbedMacro. Must be converted to JSON using.toJson().
EmbedMacroConfig Options
The EmbedMacroConfig class allows fine-grained control over how assets are embedded and accessed:
const config = EmbedMacroConfig(
generatedClassName: 'EmbedFs',
syncList: true,
recursive: true,
extension: '*',
);
generatedClassName
- Type:
String? - Default:
'EmbedFs' - Description: The name of the generated virtual file system class.
syncList
- Type:
bool? - Default:
true - Description: Whether to expose synchronous directory listing APIs (e.g.
listSync).
recursive
- Type:
bool? - Default:
true - Description: Whether to recursively include files from subdirectories when embedding assets.
extension
- Type:
String? - Default:
'*' - Description: File extensions to include during embedding.
Supported formats:
'*'— include all file types- Specific extensions such as
'.png,.json,.yaml'
How It Works
- Directory Scan: The macro scans the configured asset directory
- Filtering: Files are filtered based on the configured extensions
- Embedding: Each file is converted into a
Uint8Listin generated Dart code - Change Tracking: A
.embed_generated.jsonfile records file hashes to detect changes - Regeneration: Only changed or new files are regenerated on subsequent runs
- Runtime Access: Assets are accessed via the generated virtual file system API
Classes
- Macro Get started Installation Models Data Class Macro Asset Path Macro Global Configuration Write New Macro Capability
- Macro used to attach metadata to a Dart declaration.